#include "GBonus.h"
LPDIRECT3DTEXTURE9 CBonuses::_bounsImage;
CBonuses::CBonuses(void)
{
}
CBonuses::CBonuses(int left,int top, QuadTree *q,int kind,SOUND *sManager)
{
	_typeObj=3;
	lastTime=GetTickCount();
	vX=BONUS_SPEED;
	_isActive=true;
	_kind=kind;
	_layer=1;
	_quadTree=q;

	//top-=10;
	_pRECT=new RECT();
	_isUpDown=false;
	_pRECT->left=left;
	_hidden=-1;
	//top-=15;
	_pRECT->right=_pRECT->left+BONUS_SIZE;
	_pRECT->top=top-BONUS_SIZE;
	_pRECT->bottom=_pRECT->top+BONUS_SIZE;
	_pSoundManager=sManager;
	switch(_kind)
	{
	case 0://Coin		
		_pSprite=new CSprites(_bounsImage, true,0,4,BONUS_SIZE,BONUS_SIZE,BONUS_SIZE,left,top,0,150);
		break;
	case 1://HealthMushroom
		_direction=Direction::Letf;
		_pSprite=new CSprites(_bounsImage, false,0,1,BONUS_SIZE*3,BONUS_SIZE,BONUS_SIZE,left,top,1,0);
		break;
	case 2://Flower 
		_direction=Direction::Right;
		_pSprite=new CSprites(_bounsImage, true,0,4,0,BONUS_SIZE,BONUS_SIZE,left,top,0,150);
		break;
	case 3://Extra life
		_direction=Direction::Right;
		_pSprite=new CSprites(_bounsImage, false,1,1,BONUS_SIZE*3,BONUS_SIZE,BONUS_SIZE,left,top,1,0);
		break;
	case 4://star
		_direction=Direction::Right;
		_pSprite=new CSprites(_bounsImage, false,3,1,BONUS_SIZE*3,BONUS_SIZE,BONUS_SIZE,left,top,0,0);
		break;
	case 5://Bar
		vX=0;
		_isActive=false;
		_isUpDown = true ;
		_deltaY = 23;
		isUp=true;
		_direction=Direction::Right;
		_pRECT->right=_pRECT->left+50;
		_pRECT->top=top-16;
		_pSprite=new CSprites(_bounsImage, false,0,1,BONUS_SIZE*4,50,15,left,top,0,0);
		break;
	case 6://Poison Mushroom
		_direction=Direction::Letf;
		_pSprite=new CSprites(_bounsImage, false,2,1,BONUS_SIZE*3,BONUS_SIZE,BONUS_SIZE,left,top,1,0);
		break;
	}
	_level=0;
	_justChangeLevel=false;
	_justMove=false;
	_status=0;
	_id=countObj++;
	_screen=NULL;
}


int CBonuses::Move()
{
	if(GetTickCount()-lastTime<DELTA_TIME)
		return 0;
	lastTime=GetTickCount();
	int DeltaX=0;
	if(_kind!=0&&_kind!=2)
	{
		DeltaX=CheckLeftRight();
		_pRECT->left+=DeltaX;
		_pRECT->right+=DeltaX;
		_pSprite->_left+=DeltaX;
		if(DeltaX==0)
		{
			if(_direction==Direction::Letf)
				_direction=Direction::Right;
			else
				_direction=Direction::Letf;
		}
		if(DeltaX!=0&&_kind!=0&&_kind!=5&&_kind!=2&&_status!=1&&!_quadTree->IsOnObject(_pRECT,_id))//Allways check
		{
			_status=1;
			_isUpDown = true ; 
			_deltaY = 0;
			isUp=false;
		}
		if(_status!=1&&_kind==5)
		{
			_status=1;
			_isUpDown = true ; 
			_deltaY = 23;
			isUp=true;
		}
	}
	Jump();
	return DeltaX;
}
void CBonuses::Collision(Object *obj)
{
	if(obj->_typeObj==1&&_status!=2)//This is mario and he got the bonus
	{
		switch(_kind)
		{
		case 0://coin
			_pSoundManager->Play("GetCoin",false);
			info->CointCounter++;;
			break;
		case 1://health
			obj->ObjGetColliton(1);
			break;
		case 2://flower
			obj->ObjGetColliton(1);//level up +1
			break;
		case 3://extralife
			_pSoundManager->Play("ExtraLife",false);
			info->Life++;
			break;
		case 4://star
			_pSoundManager->Play("GetCoin",false);
			info->CointCounter+=2;
			break;
		case 5://bar
			_pSoundManager->Play("LevelUp",false);
		case 6://poision
			_pSoundManager->Play("Dying",false);
			obj->ObjGetColliton(-1);
			break;
		}
		_pSprite->_totalExistTime=1;
		_status=2;
		_pRECT->top=WINDOW_HEIGHT+10;
		_pRECT->bottom=_pRECT->top+50;
	}
}
void CBonuses::Draw(int VirtualLeft,int Style)
{
	_pSprite->Draw(VirtualLeft,0,true);
}
CBonuses::~CBonuses(void)
{
}
